package com.yl.ydk.presenter.impl

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.Html
import androidx.annotation.RequiresApi
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BaseApplication
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.SiteAricleDetailBean
import com.yl.ydk.presenter.contract.SiteAricleDetailActivityContract
import com.yl.ydk.utils.GlideUtils
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.io.InputStream
import java.net.URL

class SiteAricleDetailActivityImpl : BasePresenter<SiteAricleDetailActivityContract.View>(),
    SiteAricleDetailActivityContract.Presenter<SiteAricleDetailActivityContract.View> {


    override fun getSiteAricleDetail(context: Context, vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getSiteAricleDetail(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<SiteAricleDetailBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    @RequiresApi(Build.VERSION_CODES.N)
                    override fun onNext(t: SiteAricleDetailBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {
                            mView?.getTvName()?.text = t.data.title
                            BaseApplication.MAIN_EXECUTOR.submit {
                                val sp = Html.fromHtml(t.data.content, object : Html.ImageGetter {
                                    override fun getDrawable(source: String?): Drawable? {
                                        var inputStream: InputStream? = null
                                        try {
                                            inputStream = URL(source).content as InputStream
                                            val d = Drawable.createFromStream(inputStream, "src")
                                            d.setBounds(0, 0, d.intrinsicWidth, d.intrinsicHeight)
                                            inputStream.close()
                                            return d
                                        } catch (e: Exception) {
                                            return null
                                        }
                                    }
                                }, null)
                                mView?.getActivity()?.runOnUiThread {
                                    mView?.getTvContent()?.text = sp
                                    GlideUtils.loadToBackground(context,t.data.image,mView?.getIvUrl())
                                }
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }
}